package com.lm.upms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lm.common.core.util.R;
import com.lm.common.log.annotation.SysLog;
import com.lm.common.log.enums.BusinessType;
import com.lm.upms.api.entity.SysDept;
import com.lm.upms.service.SysDeptService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 部门信息
 *
 * @author lm
 * @date 2019/6/28 20:57
 */
@Api(tags = "部门信息")
@Slf4j
@RestController
@RequestMapping("/dept")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单
     */
    @GetMapping(value = "/tree")
    public R getTree() {
        return R.success(sysDeptService.selectTree());
    }

    /**
     * 返回当前用户树形菜单集合
     *
     * @return 树形菜单
     */
    @GetMapping(value = "/user-tree")
    public R getUserTree() {
        return R.success(sysDeptService.getUserTree());
    }

    /**
     * 获取部门
     *
     * @return 部门列表
     */
    @GetMapping("/list")
    public R listDept() {
        return R.success(sysDeptService.list(Wrappers.emptyWrapper()));
    }

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return SysDept
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        return R.success(sysDeptService.getById(id));
    }

    /**
     * 添加
     *
     * @param sysDept
     * @return
     */
    @SysLog(title = "部门管理", businessType = BusinessType.INSERT)
    @PostMapping("save")
    public R save(@Valid @RequestBody SysDept sysDept) {
        return R.success(sysDeptService.saveDept(sysDept));
    }

    /**
     * 编辑
     *
     * @param sysDept
     * @return
     */
    @SysLog(title = "部门管理", businessType = BusinessType.UPDATE)
    @PostMapping("update")
    public R update(@Valid @RequestBody SysDept sysDept) {
        return R.success(sysDeptService.updateDeptById(sysDept));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return
     */
    @SysLog(title = "部门管理", businessType = BusinessType.DELETE)
    @PostMapping("remove")
    public R removeById(@PathVariable Integer id) {
        return R.success(sysDeptService.removeDeptById(id));
    }

}
